<?php


namespace App\Http\Controllers\Admin\HongBao;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

use function Clue\StreamFilter\fun;

class HongBaoLogController extends BaseController
{

    /**
     * @api {get} /hongbao/info 红包纪录
     * @apiVersion 0.1.0
     * @apiName 红包纪录
     * @apiGroup 红包
     * @apiParam {Int}                     page 产出限制 （选填）
     * @apiParam {Int}                     page_size 历史产出限制 （选填）
     * @apiParam {Int}                     user_code 用户编号（选填）
     * @apiParam {String}                     room_name 房间名称模糊查询（选填）
     * @apiParam {String}                  nickname 用户昵称 模糊查询（选填）
     * @apiParam {Int}                     type 红包类型 0红包雨类型 1无门槛.2关注.3口令.4礼物 （选填）
     * @apiParam {Int}                     is_rain 是否为红包雨 是否为红包雨,0普通红包,1红包雨（选填）
     * @apiParam {Int}                     rush_start_time 红包开抢时间 秒级时间戳 左闭右开区间（选填）
     * @apiParam {Int}                     rush_end_time 红包开抢时间 秒级时间戳 （选填）
     * @apiParam {Int}                     pause_start_time 筛选 红包创建的时间 秒级时间戳 左闭右开区间（选填）
     * @apiParam {Int}                     pause_end_time 筛选 红包创建的时间 秒级时间戳（选填）
     * @apiParam {Int}                     start_time 筛选 红包创建的时间 秒级时间戳 左闭右开区间（选填）
     * @apiParam {Int}                     end_time 筛选 红包创建的时间 秒级时间戳（选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 2,
     * "current_page": 1,
     * "last_page": 1,
     * "list": [
     * {
     * "id": 6817,
     * "user_id": 643332,
     * "room_id": 3661,//房间id
     * "is_rain": 1,//是否为红包雨,0普通红包,1红包雨
     * "type": 0,//红包类型 1无门槛.2关注.3口令.4礼物
     * "number": 0,//红包数量
     * "money": 10000,//发出的金币
     * "grab_money": 0,//被抢的金币
     * "password": "",//口令
     * "start_time": 2021-10-09 18:09:14,//红包开始抢的时间
     * "end_time": 2021-10-09 18:09:14,//红吧结束抢的时间 红包结束时间
     * "create_time": 2021-10-09 18:09:14,//创建时间
     * "return_time": "2021-10-09 18:09:14",//退钱时间
     * "user_data": {
     * "user_id": 643332,
     * "nickname": "假如你是花我就是太皮"//用户昵称
     * "user_code": "666666"//用户编号
     * }
     * },
     * {
     * "id": 6393,
     * "user_id": 643332,
     * "room_id": 3054,
     * "is_rain": 1,
     * "type": 0,
     * "number": 5,
     * "money": 88888,
     * "grab_money": 88888,
     * "password": "",
     * "start_time": "2021-10-09 18:04:09",
     * "end_time": "2021-10-09 18:24:29",
     * "return_time": "2021-10-09 18:09:14",
     * "create_time": "2021-10-09 18:04:08",
     * "user_room": {
     * "id": 3261,
     * "room_name": "二滑大魔王"//房间名称
     * "room_code": 546664648//房间名称
     * }
     * "user_data": {
     * "user_id": 643332,
     * "nickname": "假如你是花我就是太皮"
     * "user_code": "666666"//用户编号
     * }
     * }
     * ]
     * },
     * "message": ""
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    //发红包纪录
    public function hongbaoList(Request $request)
    {
        $data = $request->only(['page', 'page_size', 'user_code', 'room_name', 'nickname', 'type', 'is_rain', 'pause_start_time', 'pause_end_time', 'rush_start_time', 'rush_end_time', 'start_time', 'end_time']);
        $va = app('validator')->make($data, [
            'page'            => 'integer|min:1',
            'page_size'       => 'integer',
            //            'user_id'         => 'integer|min:1',//用户id
            'room_name'       => 'string|max:255',//房间名称
            'user_code'       => 'integer|min:1',//用户id
            'nickname'        => 'string|max:255',//用户昵称
            'type'            => 'integer|in:0,1,2,3,4',//红包类型 0红包雨， 1无门槛.2关注.3口令.4礼物
            'is_rain'         => 'integer|in:0,1',//是否为红包雨
            'rush_start_time' => 'integer|min:0',//start_time 筛选 红包开始抢的时间
            'rush_end_time'   => 'integer|min:0',//start_time 筛选

            'pause_start_time' => 'integer|min:0',//start_time 筛选 红包结束抢的时间
            'pause_end_time'   => 'integer|min:0',//start_time 筛选

            'start_time' => 'integer|min:0',//create_time 筛选 红包创建的时间
            'end_time'   => 'integer|min:0',//create_time 筛选
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $data['page'] = $data['page'] ?? 1;
        $data['page_size'] = $data['page_size'] ?? 20;
        $where = [];
        if (isset($data['type'])) {
            $where[] = ['type', '=', $data['type']];
        }
        if (isset($data['is_rain'])) {
            $where[] = ['is_rain', '=', $data['is_rain']];
        }

        if (isset($data['rush_start_time'])) {
            $where[] = ['start_time', '>=', $data['rush_start_time']];
        }
        if (isset($data['rush_end_time'])) {
            $where[] = ['start_time', '<=', $data['rush_end_time']];
        }

        if (isset($data['pause_start_time'])) {
            $where[] = ['end_time', '>=', $data['pause_start_time']];
        }
        if (isset($data['pause_end_time'])) {
            $where[] = ['end_time', '<=', $data['pause_end_time']];
        }

        if (isset($data['start_time'])) {
            $where[] = ['create_time', '>=', $data['start_time']];
        }
        if (isset($data['end_time'])) {
            $where[] = ['create_time', '<=', $data['end_time']];
        }
        //        var_dump($where);
        $model = app('model_game_hongbao_info')
            ->orderBy('id', 'desc')
            ->with([
                'userData'    => function ($query) {
                    $query->select(['user_id', 'nickname', 'user_code']);
                }, 'userRoom' => function ($query) {
                    $query->select(['id', 'room_name', 'room_id as room_code']);
                },
            ]);

        if (isset($data['nickname'])) {
            $model = $model
                ->wherehas('userData', function ($query) use ($data) {
                    $query->where('nickname', 'like', "%{$data['nickname']}%");
                });
        }
        if (isset($data['user_code'])) {
            $model = $model
                ->wherehas('userData', function ($query) use ($data) {
                    $query->where('user_code', '=', $data['user_code']);
                });
        }
        if (isset($data['room_name'])) {
            $model = $model
                ->wherehas('userRoom', function ($query) use ($data) {
                    $query->where('room_name', 'like', "%{$data['room_name']}%");
                });
        }
        if (!empty($where)) {
            $model = $model
                ->where($where);
        }

        $field = ['id', 'user_id', 'room_id', 'is_rain', 'type', 'number', 'money', 'grab_money', 'password', 'start_time', 'end_time', 'return_time', 'create_time'];
        $list = $model->paginate($data['page_size'], $field, '', $data['page'])->toArray();
        //返回值有固定格式
        $result = [
            'total'        => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page'    => $list['last_page'] ?? 1,
            'list'         => $list['data'],
        ];

        return Response::sendData($result);
    }

    //抢红包纪录

    /**
     * @api {get} /hongbao/record 抢红包纪录
     * @apiVersion 0.1.0
     * @apiName 抢红包纪录
     * @apiGroup 红包
     * @apiParam {Int}                     page 产出限制 （选填）
     * @apiParam {Int}                     page_size 历史产出限制 （选填）
     * @apiParam {Int}                     user_code 用户编号（选填）
     * @apiParam {String}                  nickname 用户昵称 模糊查询（选填）
     * @apiParam {String}                     room_name 房间名称 模糊查询（选填）
     * @apiParam {Int}                     game_id 红包id（选填）
     * @apiParam {Int}                     start_time 筛选 红包创建的时间 秒级时间戳 左闭右开区间（选填）
     * @apiParam {Int}                     end_time 筛选 秒级时间戳 红包创建的时间 （选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 1,
     * "current_page": 1,
     * "last_page": 1,
     * "list": [
     * {
     * "id": 209923,
     * "game_id": 6395,//红包id
     * "user_id": 643816,
     * "money": 2223,//抢到的金币
     * "hits": 1,//点击次数
     * "create_time": 2021-10-08 16:58:32,//抢红包时间
     *  "user_room": {
     * "id": 3524,
     * "room_name": "巨塔略略"//房间名称
     *   "room_code": 546664648//房间名称
     * }
     * "user_data": {
     * "user_id": 643816,
     * "nickname": "亍念烟",
     * "user_code": 7875929//用户编号
     * }
     * }
     * ]
     * },
     * "message": ""
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function hongBaoRecord(Request $request)
    {
        $data = $request->only(['page', 'page_size', 'game_id', 'nickname', 'user_code', 'room_name', 'start_time', 'end_time']);
        $va = app('validator')->make($data, [
            'page'      => 'integer|min:1',
            'page_size' => 'integer',
            'game_id'   => 'integer|min:1',//红包id
            'nickname'  => 'string|max:255',//用户昵称
            'user_code' => 'integer|min:0',//红包类型 1无门槛.2关注.3口令.4礼物

            'room_name'  => 'string|max:255',//房间名称
            'start_time' => 'integer|min:1',//create_time 筛选 创建时间
            'end_time'   => 'integer|min:1',//create_time 筛选
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $data['page'] = $data['page'] ?? 1;
        $data['page_size'] = $data['page_size'] ?? 20;
        $where = [];

        if (isset($data['game_id'])) {
            $where[] = ['game_id', '=', $data['game_id']];
        }


        if (isset($data['start_time'])) {
            $where[] = ['create_time', '>=', $data['start_time']];
        }
        if (isset($data['end_time'])) {
            $where[] = ['create_time', '<=', $data['end_time']];
        }


        $model = app('model_game_hongbao_record')
            ->orderBy('id', 'desc')
            ->with([
                'userData'    => function ($query) {
                    $query->select(['user_id', 'nickname', 'user_code']);
                }, 'userRoom' => function ($query) {
                    $query->select(['id', 'room_name', 'room_id as room_code']);
                },
            ]);
        if (isset($data['nickname'])) {
            $model = $model
                ->wherehas('userData', function ($query) use ($data) {
                    $query->where('nickname', 'like', "%{$data['nickname']}%");
                });
        }
        if (isset($data['user_code'])) {
            $model = $model
                ->wherehas('userData', function ($query) use ($data) {
                    $query->where('user_code', '=', $data['user_code']);
                });
        }
        if (isset($data['room_name'])) {
            $model = $model
                ->wherehas('userRoom', function ($query) use ($data) {
                    $query->where('room_name', 'like', "%{$data['room_name']}%");
                });
        }
        if (!empty($where)) {
            $model = $model
                ->where($where);
        }
        $field = ['id', 'game_id', 'user_id', 'room_id', 'money', 'hits', 'create_time'];
        $list = $model->paginate($data['page_size'], $field, '', $data['page'])->toArray();
        //返回值有固定格式
        $result = [
            'total'        => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page'    => $list['last_page'] ?? 1,
            'list'         => $list['data'],
        ];

        return Response::sendData($result);
    }

}